package com.kuang.demo.service.wx;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.demo.bean.wx.CommentExample;
import com.kuang.demo.bo.wx.CommentListBo;
import com.kuang.demo.mapper.wx.CommentMapper;
import com.kuang.demo.vo.wx.Comment4ListVo;
import com.kuang.demo.vo.wx.CommentListVo;
import com.kuang.demo.vo.wx.Count4CommentVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxCommentServiceImp implements WxCommentService{

    @Autowired
    CommentMapper commentMapper;

    @Override
    public Count4CommentVo count(Integer valueId, Byte type) {
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andValueIdEqualTo(valueId)
                .andTypeEqualTo(type);
        long allCount = commentMapper.countByExample(commentExample);

        CommentExample commentExample2 = new CommentExample();
        commentExample2.createCriteria().andValueIdEqualTo(valueId)
                .andTypeEqualTo(type).andHasPictureEqualTo(true);
        long hasPicCount = commentMapper.countByExample(commentExample2);
        return new Count4CommentVo(hasPicCount,allCount);
    }

    @Override
    public CommentListVo list(CommentListBo commentListBo) {
        //分页
        PageHelper.startPage(commentListBo.getPage(),commentListBo.getSize());
        //得到有关评论数组
        List<Comment4ListVo> commentList = commentMapper.queryCommentLisByTypeAndHasPicAndValueId(
                commentListBo.getType(), commentListBo.getShowType(),commentListBo.getValueId());

        // TODO: 2020/11/5 利用shio 取得 nickName，avatarUrl
        //         然后赋值给每一个 comment4ListVo的userInfo
//        Subject subject = SecurityUtils.getSubject();
//        Object principal = subject.getPrincipal(); 无法长久保持可以取出，登录三分钟后再来取就是null
        for (Comment4ListVo comment4ListVo : commentList) {
            comment4ListVo.getUserInfo().setNickName("测试用户");
            comment4ListVo.getUserInfo().setAvatarUrl("imageView2/1/w/80/h/80");
        }

        //将数组，页数信息赋值给commentListVo
        PageInfo<Comment4ListVo> listPageInfo = new PageInfo<>(commentList);
        CommentListVo commentListVo = new CommentListVo(listPageInfo.getTotal(),
                commentListBo.getPage(), commentList);
        return commentListVo;

    }

}
